<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function employee_list_page()
{
    @$output = drupal_render(drupal_get_form('employee_list_form'));
    return $output;
}

function employee_list_form($form, &$form_state)
{
    $form['name'] = array(
        '#attributes' => array('placeholder' => '姓名'),
        '#type' => 'textfield',
        '#default_value' => !empty($form_state['values']['name']) ? $form_state['values']['name'] : '',
    );
    $form['telephone'] = array(
        '#attributes' => array('placeholder' => '手机'),
        '#type' => 'textfield',
        '#maxlength' => 11,
        '#default_value' => !empty($form_state['values']['telephone']) ? $form_state['values']['telephone'] : '',
    );
    $table['#markup'] =data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function employee_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
    $output = '';
    //表头
    global $user;
    $user_info = _get_user_info();
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        array('data' => '姓名', 'class' => ['essential employee_name']),
        array('data' => '部门', 'class' => ['essential department'], 'field' => 'department_name'),
        array('data' => '职务', 'class' => ['essential position'], 'field' => 'position'),
        array('data' => '经理', 'class' => ['essential manager_name']),
        array('data' => '区域', 'class' => ['essential juris'], 'field' => 'juris_name'),
        array('data' => '公司', 'class' => ['essential company'], 'field' => 'company'),
        array('data' => '手机', 'class' => ['essential cell']),
        //array('data' => '邮件地址', 'class' => ['email']),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $query = db_select('employee', 'E')->extend('PagerDefault')->extend('TableSort');
    if (!empty($form_state['values']['name'])) {//模糊姓名查询条件
        $query->condition('E.employee_name', '%' . db_like($form_state['values']['name']) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['telephone'])) {//模糊电话查询条件
        $query->condition('E.cell', '%' . db_like($form_state['values']['telephone']) . '%', 'LIKE');
    }
    $query->leftjoin('employee', 'M', 'M.employee_id=E.manager_uid');
    $query->leftjoin('department', 'D', 'D.department_id=E.department_id');
    $query->leftjoin('juris', 'J', 'J.juris_id=E.juris_id');

    $query->fields('E', array('employee_id', 'employee_name', 'position', 'cell', 'company', 'author_uid'));

    $query->addField('M', 'employee_name', 'manager_name');
    $query->addField('D', 'department_name');
    $query->addField('J', 'juris_name');

    if(empty($form_state['values']) || empty($form_state['values']['order'])) {
        $query->orderBy('E.timestamp', 'DESC');
    }
    if ($user_info['role'] != 'administrator') {
        $query->condition('E.employee_id', $user->uid, '=');
    }
    if(!user_access('view all employee') && user_access('view own employee')){
        $query->condition('E.author_uid',$user->uid, '=');
    }

    $query->limit(variable_get("list_page_item_number", 5));

    $employeeInfo = $query->orderByHeader($header)->execute();

    $rows = array();
    //表内容
    $n = 1;
    foreach ($employeeInfo as $row) {
        if (nonghua_user_access('employee', 'delete', $row->employee_id, $row->author_uid, $row->employee_id)) {
            $disabled = ' class="listViewTableCheck" ';
        } else{
            $disabled = ' disabled="true" ';
        }
        $rows[] = array(
            array('data' => "<input type='checkbox'".$disabled." value ='" . $row->employee_id . "' name='" . $row->employee_id . "'/>" . $n++, 'class' => 'essential no'),
            array('data' => $row->employee_name, 'class' => ['essential employee_name']),
            array('data' => $row->department_name, 'class' => ['essential department_name']),
            array('data' => $row->position, 'class' => ['essential position']),
            array('data' => $row->manager_name, 'class' => ['essential manager_name']),
            array('data' => $row->juris_name, 'class' => ['essential juris']),
            array('data' => $row->company, 'class' => ['essential company']),
            array('data' => $row->cell, 'class' => ['essential cell']),
            array('data' => '<a href="'. base_path() . 'employee/edit/' . $row->employee_id . '" data-toggle="modal" target="_top"><img width="20px" height="20px" src="' . base_path() . 'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more'),
        );
    }


    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    /*	 * *按钮** */
    if (sizeof($rows) > 0 && (user_access('delete own employee') ||  user_access('delete all employee'))) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
    $output .= theme('pager');
    return $output;
}

function employee_delete()
{
    ajax_table_delete('employee', 'employee_id', 'true');
}